﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Customers
{
    /// <summary>
    /// This object represents the methods of a Role.
    /// </summary>
    internal class RoleDAO
    {

        #region general
        private static Role LoadObj(IDataReader datareader)
        {
            Role obj = new Role();
            obj.RoleID = MpDataHelp.GetInt(datareader, "RoleID");
            obj.Name = MpDataHelp.GetString(datareader, "Name");
            obj.Active = MpDataHelp.GetBoolean(datareader, "Active");
            obj.Deleted = MpDataHelp.GetBoolean(datareader, "Deleted");

            return obj;
        }
        #endregion

        public static int Role_Add(Role obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Role_Add");
            db.AddInParameter(dbCommand, "RoleID", DbType.Int32, obj.RoleID);
            db.AddInParameter(dbCommand, "Name", DbType.String, obj.Name);
            db.AddInParameter(dbCommand, "Active", DbType.Boolean, obj.Active);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, obj.Deleted);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@RoleID"));
            return -1;
        }
        public static int Role_Update(Role obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Role_Update");
            db.AddInParameter(dbCommand, "RoleID", DbType.Int32, obj.RoleID);
            db.AddInParameter(dbCommand, "Name", DbType.String, obj.Name);
            db.AddInParameter(dbCommand, "Active", DbType.Boolean, obj.Active);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, obj.Deleted);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Role_Delete(Int32 RoleID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Role_Delete");
            db.AddInParameter(dbCommand, "RoleID", DbType.Int32, RoleID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Role> Role_List(int RoleID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Role_List");
            db.AddInParameter(dbCommand, "RoleID", DbType.Int32, RoleID);
            List<Role> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Role>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
